package com.rr.sc.dao.impl.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.rr.sc.dao.AccountDao;
import com.rr.sc.model.Account;

@Repository
public class AccountDaoImpl extends AbstractDao implements AccountDao {
    public void saveOrUpdate(Account account) {
	getCurrentSession().saveOrUpdate(account);
	getCurrentSession().flush();
    }

    public Account get(Long id) {
	return (Account) getCurrentSession().get(Account.class, id);
    }

    public void delete(Account account) {
	getCurrentSession().delete(account);
    }

    @SuppressWarnings("unchecked")
    public List<Account> getByUserIds(List<Long> userIds) {
	if (!CollectionUtils.isEmpty(userIds)) {
	    return findByNamedParam("from Account where user.id in (:userIds)", new String[] { "userIds" }, new Object[] { userIds });
	} else {
	    return find("from Account where user.id is null");
	}
    }
}
